package com.yiren.dbaa.module.param_config.services.impl;

import android.content.Context;

import com.google.gson.Gson;
import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.api.SaveConfigApi;
import com.yiren.backstage.config.bean.BaseParamConfigBean;
import com.yiren.backstage.config.bean.device.CarSignalConfigBean;
import com.yiren.backstage.config.bean.device.DeviceStudyConfigBean;
import com.yiren.backstage.config.bean.device.PracticeCarCheckConfigBean;
import com.yiren.backstage.config.bean.device.SignalSourceConfigBean;
import com.yiren.backstage.config.bean.device.StationParamConfigBean;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.config.bean.device.UserRiskConfigBean;
import com.yiren.backstage.config.enums.ConfigNameEnum;
import com.yiren.dbaa.constant.AddressConstant;
import com.yiren.dbaa.constant.G;
import com.yiren.dbaa.module.param_config.bean.ParamConfigBean;
import com.yiren.dbaa.module.param_config.services.IParamConfigService;
import com.yiren.dbaa.net.OkHttpUtils;
import com.yiren.dbaa.net.interf.StringCallback;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WANGRONGXIANG
 * on 2022/5/25
 * Describe 参数配置上传下载实现类
 */
public class ParamConfigServiceImpl implements IParamConfigService {

    private Gson gs;

    public ParamConfigServiceImpl() {
        gs = new Gson();
    }

    @Override
    public void uploadParamConfig(Context context, String key, int type, BaseParamConfigBean baseParamConfigBean) {
        setParamConfigState(context, key, baseParamConfigBean, false);
        String id = "";
        if (type == 1) {  //设备参数
            id = LoadConfigApi.loadStationParamConfig(context).getHostNumber();
            if (StringUtils.isEmpty(id)) id = G.deviceNumber;
        } else {  //考场参数
            if (null != LoadConfigApi.loadUserParamConfig(context).getSite())
                id = LoadConfigApi.loadUserParamConfig(context).getSite().getSiteCode();
        }
        if (StringUtils.isEmpty(id)) return;
        List<ParamConfigBean> list = new ArrayList<>();
        list.add(new ParamConfigBean(key, gs.toJson(baseParamConfigBean), type, id));
        OkHttpUtils.getInstance().postJsonString(context, AddressConstant.uploadParamConfig(), gs.toJson(list), new StringCallback() {
            @Override
            public void getString(String result) {
                setParamConfigState(context, key, baseParamConfigBean, true);
            }
        });
    }

    /**
     * 设置参数配置状态
     *
     * @param success true:上传成功  false:上传失败
     */
    private void setParamConfigState(Context context, String key, BaseParamConfigBean baseParamConfigBean, boolean success) {
        baseParamConfigBean.setState(success ? 0 : 1);
        switch (ConfigNameEnum.valueOf(key)) {
            case CAR_SIGNAL_CONFIG:
                CarSignalConfigBean carSignalConfig = (CarSignalConfigBean) baseParamConfigBean;
                SaveConfigApi.saveCarSignalConfig(context, carSignalConfig);
                break;
            case DEVICE_STUDY_CONFIG:
                DeviceStudyConfigBean deviceStudyConfig = (DeviceStudyConfigBean) baseParamConfigBean;
                SaveConfigApi.saveDeviceStudyConfig(context, deviceStudyConfig);
                break;
            case PRACTICE_CAR_CHECK_CONFIG:
                PracticeCarCheckConfigBean practiceCarCheckConfig = (PracticeCarCheckConfigBean) baseParamConfigBean;
                SaveConfigApi.savePracticeCarCheckConfig(context, practiceCarCheckConfig);
                break;
            case SIGNAL_SOURCE_CONFIG:
                SignalSourceConfigBean signalSourceConfig = (SignalSourceConfigBean) baseParamConfigBean;
                SaveConfigApi.saveSignalSourceConfig(context, signalSourceConfig);
                break;
            case STATION_PARAM_CONFIG:
                StationParamConfigBean stationParamConfig = (StationParamConfigBean) baseParamConfigBean;
                SaveConfigApi.saveStationParamConfig(context, stationParamConfig);
                break;
            case USER_PARAM_CONFIG:
                UserParamConfigBean userParamConfig = (UserParamConfigBean) baseParamConfigBean;
                SaveConfigApi.saveUserParamConfig(context, userParamConfig);
                break;
            case USER_RISK_CONFIG:
                UserRiskConfigBean userRiskConfig = (UserRiskConfigBean) baseParamConfigBean;
                SaveConfigApi.saveUserRiskConfig(context, userRiskConfig);
                break;
        }
    }

    /**
     * 保存从服务器下载的参数
     *
     * @param context
     * @param paramConfig
     */
    private void saveParamConfig(Context context, ParamConfigBean paramConfig) {
        String name = paramConfig.getName();
        switch (ConfigNameEnum.valueOf(name)) {
            case CAR_SIGNAL_CONFIG:
                CarSignalConfigBean carSignalConfig = gs.fromJson(paramConfig.getJson(), CarSignalConfigBean.class);
                SaveConfigApi.saveCarSignalConfig(context, carSignalConfig);
                break;
            case DEVICE_STUDY_CONFIG:
                DeviceStudyConfigBean deviceStudyConfig = gs.fromJson(paramConfig.getJson(), DeviceStudyConfigBean.class);
                SaveConfigApi.saveDeviceStudyConfig(context, deviceStudyConfig);
                break;
            case PRACTICE_CAR_CHECK_CONFIG:
                PracticeCarCheckConfigBean practiceCarCheckConfig = gs.fromJson(paramConfig.getJson(), PracticeCarCheckConfigBean.class);
                SaveConfigApi.savePracticeCarCheckConfig(context, practiceCarCheckConfig);
                break;
            case SIGNAL_SOURCE_CONFIG:
                SignalSourceConfigBean signalSourceConfig = gs.fromJson(paramConfig.getJson(), SignalSourceConfigBean.class);
                SaveConfigApi.saveSignalSourceConfig(context, signalSourceConfig);
                break;
            case STATION_PARAM_CONFIG:
                StationParamConfigBean stationParamConfig = gs.fromJson(paramConfig.getJson(), StationParamConfigBean.class);
                SaveConfigApi.saveStationParamConfig(context, stationParamConfig);
                break;
            case USER_PARAM_CONFIG:
                UserParamConfigBean userParamConfig = gs.fromJson(paramConfig.getJson(), UserParamConfigBean.class);
                SaveConfigApi.saveUserParamConfig(context, userParamConfig);
                break;
        }
    }

}
